<?php
/**
 * User: jewens
 */
class Withdrawals extends MY_Admin_Controller
{
    public function index()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	
    	$start_time = trim($this->input->get_post('start_time'));
    	$end_time = trim($this->input->get_post('end_time'));
    	$uid = trim($this->input->get_post('uid'));
    	
    	$where = array();
    	
    	if($start_time){
    		$where['fw.create_time>'] = $start_time;
    	}
    	
    	if($end_time){
    		$where['fw.create_time<'] = $end_time;
    	}
    	
    	if($uid){
    		$where['fw.uid'] = $uid;
    	}
    	
        $this->load->model('finance/model_finance_withdrawals','finance_withdrawals');
        
        
        if($this->input->get_post('submit') == 'export')
        {
        	$this->db->select('fw.create_time, fw.uid, fw.uname, fw.amount, fuca.balance, ura.realname, ura.id_card, fbbc.card_no, fbbc.bank, fbbc.bank_branch,fbbc.cardholder,fbc.rate')->from($this->db->dbprefix.'finance_withdrawals as fw');
	    	$this->db->join($this->db->dbprefix.'user_realname_auth as ura','ura.uid = fw.uid and ura.status=1', 'left');
	    	$this->db->join($this->db->dbprefix.'finance_user_capital_account as fuca','fuca.uid = fw.uid', 'left');
	    	$this->db->join($this->db->dbprefix.'finance_bind_bank_card as fbbc','fbbc.bid=fw.bid and fbbc.uid = fw.uid and fbbc.status=1', 'left');
	    	$this->db->join($this->db->dbprefix.'finance_bank_conf as fbc','fbc.bid=fw.bid and fbbc.uid = fbbc.bank_id', 'left');
	    	$this->db->where($where);
	        $financeInfo = $this->db->get()->result_array();
        	 
        	$export_data[] = array('时间', '时光号', '实名', '身份证', '开户行', '持卡人', '帐号', '帐户可提余额', '申请提现金额', '提现手续费', '应发金额');
        	 
        	foreach($financeInfo as $v)
        	{
        		$fee = round($v['amount']*0.006, 2);
        		$export_data[] = array(
        				$v['create_time'],
        				$v['uid'],
        				$v['realname'],
        				$v['id_card'],
        				$v['bank'].$v['bank_branch'],
        				$v['cardholder'],
        				$v['card_no'],
        				$v['balance'],
        				$v['amount'],
        				$fee,
        				$v['amount']-$fee,
        		);
        	}
        	 
        	$this->load->service('common/service_common_helper');
        	$this->service_common_helper->create_xls($export_data, $filename='withdrawals_record_'.date('Y-m-d').'.xls');
        }
        
        $total_rows = $this->finance_withdrawals->count();

        //分页
        $this->load->library('pagination');
         
        $config['total_rows'] = $total_rows;
        $config['per_page'] = 20;
        $config['base_url'] = '/finance/Withdrawals/index';
         
        $this->pagination->initialize($config);
        $show_page = $this->pagination->create_links();
        
        //读取数据
        $offset = ($page-1) * $config['per_page'];
        //$financeInfo = $this->finance_withdrawals->lists($offset,  $config['per_page']);
        $this->db->select('fw.create_time, fw.uid, fw.uname, fw.amount, fuca.balance, ura.realname, ura.id_card, fbbc.card_no, fbbc.bank, fbbc.bank_branch,fbbc.cardholder,fbc.rate')->from($this->db->dbprefix.'finance_withdrawals as fw');
    	$this->db->join($this->db->dbprefix.'user_realname_auth as ura','ura.uid = fw.uid and ura.status=1', 'left');
    	$this->db->join($this->db->dbprefix.'finance_user_capital_account as fuca','fuca.uid = fw.uid', 'left');
    	$this->db->join($this->db->dbprefix.'finance_bind_bank_card as fbbc','fbbc.bid=fw.bid and fbbc.uid = fw.uid and fbbc.status=1', 'left');
    	$this->db->join($this->db->dbprefix.'finance_bank_conf as fbc','fbc.bid=fw.bid and fbbc.uid = fbbc.bank_id', 'left');
    	$this->db->where($where);
    	$this->db->limit($config['per_page'], $offset);
        $financeInfo = $this->db->get()->result_array();

        $amount_total = $this->db->select('sum(fw.amount) as amount_total')->from($this->db->dbprefix.'finance_withdrawals as fw')
        	->where($where)
        	->limit(1)
            ->get()
            ->unbuffered_row('array');
        //p($financeInfo);
        $data = array(
            'financeInfo' => $financeInfo,
            'show_page'   => $show_page,
        	'total_rows'  => $config['total_rows'],
        	'amount_total'=> $amount_total,
        );

        $this->load->view('admin/finance/withdrawals/index',$data);
    }
}